<?php

class Controller_Test_Dao extends Controller_Test_Abstract
{
    protected $_urlLink = '/_test/dao/';

    private $_dao;

    public $autoRender = false;

    public function init()
    {
        $this->_dao = Dao('Blog');
        echo '<h1>请先在my_blog表中正常插入数据</h1>';
        echo '<pre>';
    }

    public function rowAction()
    {
        echo '<h1>row</h1>';
        $row = $this->_dao->row(array('ip' => '127.0.0.1'));
        var_dump($row);
    }

    public function rowByPkAction()
    {
        echo '<h1>rowByPk</h1>';
        // 获取数据s
        $tmp = $this->_dao->row(array('ip' => '127.0.0.1'));

        $row = $this->_dao->rowByPk($tmp['id']);
        var_dump($row);
    }

    public function countAction()
    {
        echo '<h1>count</h1>';
        $cnt1 = $this->_dao->count(array('created' => array('>', 0)));
        $cnt2 = $this->_dao->count('created > 0');

        var_dump($cnt1);
        var_dump($cnt2);
    }

    public function findAction()
    {
        echo '<h1>find no fields, fetchAssoc</h1>';
        $fineArr = $this->_dao->find(array('ip' => ''));
        var_dump($fineArr);

        echo '<h1>find special field, fetchAssoc </h1>';
        $fineArrF = $this->_dao->find(array('ip' => ''), 'created DESC', "`title`, `content`");
        var_dump($fineArrF);

        echo '<h1>find * fields, fetchAll</h1>';

        $fineFetchArr = $this->_dao->find(array('ip' => ''), 'created DESC', '*');
        var_dump($fineFetchArr);
    }

    public function findByPageAction()
    {
        // 分页
        $pageSize = 2;
        $page     = max(1, $this->get('page'));
        $start    = ($page - 1) * $pageSize;

        $list = $multipage = '';

        $total = $this->_dao->count();
        if ($total) {
            $list      = $this->_dao->findByPage(null, $start, $pageSize);
            $pager     = new Com_Pager($page, $pageSize, $total, $this->_urlLink . 'find-by-page/?page=%page%');
            $multipage = $pager->html();
        }

        print_r($list);
        echo $multipage;
    }

    public function pairsAction()
    {
        echo '<h1>pairs</h1>';
        var_dump($this->_dao->pairs(array('ip' => '127.0.0.1')));
    }

    public function namesAction()
    {
        echo '<h1>获取的names 内容 为初始时设置的_nameField</h1>';
        $arr = $this->_dao->names('1');
        var_dump($arr);
    }

    public function insertAction()
    {
        $setArr = array(
            'title'   => 'insert title ' . uniqid(),
            'content' => 'insert content ' . uniqid(),
            'created' => time(),
            'updated' => time(),
        );
        var_dump($this->_dao->insert($setArr));
    }

    public function replaceAction()
    {
        $row = $this->_dao->row(array());
        echo "old<BR>";
        var_dump($row);
        echo "replace res<BR>";
        $row['title'] = 'replace' . time() . rand(1, 100);
        var_dump($this->_dao->replace($row));
        echo "new<BR>";
        var_dump($this->_dao->rowByPk($row['id']));
    }

    public function batchInsertAction()
    {
        echo "<h1>batchInsert</h1>";
        $setArr = array(
            array(
                'title'   => 'batchInsert title ' . uniqid(),
                'content' => 'batchInsert content ' . uniqid(),
            ),
            array(
                'title'   => 'batchInsert title ' . uniqid(),
                'content' => 'batchInsert content ' . uniqid(),
            ),
        );
        var_dump($this->_dao->batchInsert($setArr));

        echo "<h1>find insert data</h1>";
        foreach ($setArr as $val) {
            $row = $this->_dao->find(array('title' => $val['title']), null, '*', 'fetchAll');
            $tmpArr[] = $row[0];
        }
        var_dump($tmpArr);

        echo "<h1>batchInsert replace</h1>";
        foreach ($setArr as $key => $val) {
            $tmpArr[$key]['title'] = $val['title'] . "batch replacce";
        }

        var_dump($this->_dao->batchInsert($tmpArr, true));

        echo '<h1>replace find</h1>';

        foreach ($tmpArr as $val) {
            $tmpArr2[] = $this->_dao->find(array('title' => $val['title']), null, '*', 'fetchAll');
        }
        var_dump($tmpArr2);
    }

    public function updateAction()
    {
        $setArr = array(
            'title'   => 'insert title ' . uniqid(),
            'content' => 'insert content ' . uniqid(),
        );
        echo "<h1>insert data</h1>";
        var_dump($this->_dao->insert($setArr));

        echo "<h1>read data</h1>";
        var_dump($readdata = $this->_dao->row(array('title' => $setArr['title'])));

        echo "<h1>update data</h1>";

        var_dump($this->_dao->update(array('title' => $readdata['title'] . ' update'), array('title' => $readdata['title'])));

        echo "<h1>read data</h1>";
        var_dump($this->_dao->find(array('id' => $readdata['id'])));
    }

    public function updateByPkAction()
    {
        $setArr = array(
            'title'   => 'insert title ' . uniqid(),
            'content' => 'insert content ' . uniqid(),
        );
        echo "<h1>insert data</h1>";
        var_dump($this->_dao->insert($setArr));

        echo "<h1>read data</h1>";
        var_dump($readdata = $this->_dao->row(array('title' => $setArr['title'])));

        echo "<h1>update data</h1>";

        var_dump($this->_dao->updateByPk(array('title' => $readdata['title'] . ' update'), $readdata['id']));

        echo "<h1>read data</h1>";
        var_dump($this->_dao->row(array('id' => $readdata['id'])));
    }

    public function updatePositiveSubAction()
    {
        $setArr = array(
            'created' => array('-', 10, 'POSITIVE_SUB'),
            'updated' => array('-', 10),
        );
        var_dump($this->_dao->updateByPk($setArr, 8));
    }

    public function deleteAction()
    {
        $readdata = $this->_dao->row(array());
        echo '<h1>read</h1>';
        var_dump($readdata);

        echo '<h1>delete</h1>';
        var_dump($this->_dao->delete(array('title' => addslashes($readdata['title']))));

        echo '<h1>read</h1>';
        var_dump($this->_dao->row(array('title' => addslashes($readdata['title']))));
    }

    public function deleteByPkAction()
    {
        $readdata = $this->_dao->row(array());
        echo '<h1>read</h1>';
        var_dump($readdata);

        echo '<h1>delete</h1>';
        var_dump($this->_dao->deleteByPk($readdata['id']));

        echo '<h1>read</h1>';
        var_dump($this->_dao->row(array('title' => addslashes($readdata['title']))));
    }

    public function incrementAction()
    {
        $readdata = $this->_dao->row(array());
        echo '<h1>read</h1>';
        var_dump($readdata);
        echo '<h1>increment</h1>';
        var_dump($this->_dao->increment('created', '1', array('id' => $readdata['id'])));

        echo '<h1>read</h1>';
        var_dump($this->_dao->row(array('id' => $readdata['id'])));
    }

    public function decrementAction()
    {
        $readdata = $this->_dao->row(array());
        echo '<h1>read</h1>';
        var_dump($readdata);
        echo '<h1>decrement</h1>';
        var_dump($this->_dao->decrement('created', '1', array('id' => $readdata['id'])));

        echo '<h1>read</h1>';
        var_dump($this->_dao->row(array('id' => $readdata['id'])));
    }

    public function decrementxAction()
    {
        $readdata = $this->_dao->row(array());
        echo '<h1>read</h1>';
        var_dump($readdata);
        echo '<h1>decrementx</h1>';
        var_dump($this->_dao->decrementx('created', '10', array('id' => $readdata['id'])));

        echo '<h1>read</h1>';
        var_dump($this->_dao->row(array('id' => $readdata['id'])));
    }
}